package com.grj.inventory.consumer;

import com.grj.common.model.Order;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

@Component
public class InventoryConsumer {
    private final KafkaTemplate<String, Object> kafkaTemplate;

    public InventoryConsumer(KafkaTemplate<String, Object> kafkaTemplate) {
        this.kafkaTemplate = kafkaTemplate;
    }

    @KafkaListener(topics = "payment-success", groupId = "inventory-service")
    public void handlePaymentSuccess(Order order) {
        System.out.println("📦 [InventoryService] Deducting inventory for " + order.getOrderId());
        // simulate success
        order.setStatus("COMPLETED");
        kafkaTemplate.send("inventory-updated", order.getOrderId(), order);
        System.out.println("📤 [InventoryService] Sent inventory-updated for " + order.getOrderId());
    }
}
